<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>入库回退管理</title>
<!-- 导入jquery核心类库 -->
  <%@include file="../include/jqueryeasyui.jsp"%>
 

<script type="text/javascript">
	function doAdd(){
	
		$('#outputWindow').form('clear');
		$('#outputWindow *').trigger('blur');// 离焦
		$('#outputWindow').window("open").window('setTitle', '入库');
	}
	
	function getQueryParams(queryParams) {  
		var searchcontent = $('#searchcontent').val();
	     queryParams.oBProposerName = searchcontent;  
	     return queryParams;  
	   
	 }  
	
	 //增加查询参数，重新加载表格  
    function reloadgrid() {  
  
        //查询参数直接添加在queryParams中      
        var queryParams = $('#grid').datagrid('options').queryParams;  
        getQueryParams(queryParams);  
        $('#grid').datagrid('options').queryParams = queryParams;  
        $("#grid").datagrid('load');  
    }  
	 
	function doSearch(){
		
		//获取查询参数
		//查询
		//刷新easyui 
  		reloadgrid();
		
		
	}
	
	function doEdit(){
		var rowData = $('#grid').datagrid('getSelected');  
        
        if (rowData == null) {  
            $.messager.alert("提示", "请选择要修改的行！", "info");  
        } else{
	        $("input[name='sId']").val(rowData.sId);
			$("input[name='sName']").val(rowData.sName);
			$("input[name='sEmail']").val(rowData.sEmail);
			$("input[name='sPhone']").val(rowData.sPhone);
			$('#sSex').combobox('setValue', rowData.sSex);
			$('#sStatus').combobox('setValue', rowData.sStatus);
	 		$('#dept').combobox('select', rowData.sDeptId);

			// 弹出窗口
			  
			$("#addStandardWindow").window('open').window('setTitle', '修改员工');
        } 
	}
	

	// 定义列
	var columns = [ [ {
		field : 'oBTime',
		title : '入库时间',
		width : 120,	
		
		formatter:formatterDate
	}, {
		field : 'oBPNumber',
		title : '入库数量',
		width : 30,
		
	}, {
		field : 'oBKuGuanName',
		title : '经手人',
		width : 120,
		
	}, {
		field : 'oBProposerName',
		title : '申请入库人',
		width : 120,
		
	}, {
		field : 'oBType',
		title : '退回原因',
		width : 90,
		formatter:formatteroBType
		
	}, {
		field : 'oBBuyerName',
		title : '客户名称',
		width : 90,
		
	}, {
		field : 'oBReMark',
		title : '备注',
		width : 160,
		
	}, {
		field : 'oBPCodes',
		title : 'E尺SN',
		width : 160,
		
	}, {
		field : 'oBIsDone',
		title : '是否办理',
		width : 160,
		
		formatter:formateroBIsDone
	}, {
		field : 'oBid',
		title : '操作',
		width : 160,
		
		formatter:formatterCaoZuo
	} ] ];
	
	function formatteroBType(value){
		if(value==0){
			return "员工自用退回"
		}else if(value==1){
			return "销售出货退回"
		}
	}
	function formatterCaoZuo(value,row,index){
		if(row.oBIsDone==false && sessionstaff.sId==row.oBKuGuanId){


			return "<a onclick=doProcess("+index+") href=#>处理</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onclick=doDel("+index+") href=#>删除</a>";
		}else{
			return "";
		}
	}
	function doProcess(index){
	    $('#grid').datagrid('selectRow',index);// 关键在这里  
	    var row = $('#grid').datagrid('getSelected'); 
	    if(row){ 
			location.href="${pageContext.request.contextPath}/page.orderBackdetails.ashx?oBId="+row.oBId;

		}
	}
	
	function doDel(index){
	    $('#grid').datagrid('selectRow',index);// 关键在这里  
	    var row = $('#grid').datagrid('getSelected'); 
	    if(row){ 
 
		     $.messager.confirm("操作提示", "确认删除 当前记录？", function (data) {  
		            if (data) {  
		            	$.post("${pageContext.request.contextPath}/page.orderbackdel.ashx?oBId="+row.oBId,function(data){
			    			
			    			if(data.status == "1"){
			    				// 成功
			    				$.messager.alert('消息','操作成功 ','info');
			    				reloadgrid();
			    				
			    			
			    			}else{
			    					$.messager.alert('消息','<br>操作失败','info');
			    				
			    			}
			    				
			    		}); 
		            }  
		            else {  
		                 
		            }  
		        }); 


		}
	}

	
	function formatterDate(value,row,index){
		if(value==''||null==value){
			return '';
		}
		var now = new Date(parseInt(value) * 1000);  
		var year=now.getYear()+1900; 
		var month=now.getMonth()+1; 
		var date=now.getDate(); 
		var hour=now.getHours(); 
		var minute=now.getMinutes();
		minute= (minute > 9) ? minute=minute : minute='0'+minute;
		//var second=now.getSeconds(); 
		return year+"-"+month+"-"+date+" "+hour+":"+minute; 
	}
	
	 function formateroBIsDone(value,row,index){
		if(value==false){
			return "否";
		}else{
			return "是";
		}
	} 
	


	


	$(function(){
		$('#add').click(doAdd);
/* 		$('#edit').click(doEdit);
 */		$('#search').click(doSearch);
 		$("#select_use").change(
			function() {
				var selected = $("#select_use").val();
				if(selected==1){
					$("#customer_tr").show();
				}else{
					$("#customer_tr").hide();
				}
			}		
		)
	});
	$(function(){
		// 先将body隐藏，再显示，不会出现页面刷新效果
		$("body").css({visibility:"visible"});
		
		// 收派标准数据表格
		$('#grid').datagrid( {
			iconCls : 'icon-forward',
			fit : true,
			border : false,
			rownumbers : true,
			singleSelect:true,
			striped : true,
			toolbar : '#tb',
			url : "${pageContext.request.contextPath}/page.orderBackList.ashx",
			idField : 'sId',
			columns : columns,
			pageList: [1,10,20,30],
			pagination:true,
			view:myview,
			emptyMsg:"<font size=5>暂无记录</font>"
			
		});
		
		//初始化员工窗口
		$('#outputWindow').window({
            title: '入库',
            width: 400,
            modal: true,
            shadow: true,
            closed: true,
            height: 400,
            resizable:false
            
        });
		
		
		
		//提交表单
		$('#save').click(function(){
				// 校验
				var a = $('#select_use').val();
				if(a==null){
					$.messager.alert('消息','请输入返库原因','info');
					return;
				}else if(a==1){
					var b = $('#oOBuyer').combobox('getValue');
 					if(b==null||b==''){
						$.messager.alert('消息','请输入返库客户','info');
						return;
					}
				}
				
			 
				 
				 var shenqingrenV = comboboxIsSelected('shenqingren');
				 var kuguanV = comboboxIsSelected('kuguan');
				 var buyerV = comboboxIsSelected('oOBuyer');
				 
				 
 				var comboboxV = 0;
				
				if(a==1){
					if(!(shenqingrenV&&buyerV&&kuguanV)){comboboxV=1;} 
				}else{
					if(!(shenqingrenV&&kuguanV)){comboboxV=1;} 
				}
				
 				if(comboboxV==1){
					$.messager.alert('消息','请在下拉框中选取项目！','info');
  					return;
				}
				
				
 
				
				
				
				if($('#sform').form('validate')){
					// 1 、获得form 数据
					var params = $('#sform').serialize();
					// 2、将数据发送给服务器 
					$.post("${pageContext.request.contextPath}/page.saveOrderBack.ashx", params , function(data){
						if(data == "success"){
							// 成功
							$.messager.alert('消息','操作成功 ','info');
							// 关闭窗口
							$('#outputWindow').window('close');
							// 清除表单数据
							$('#outputWindow').form('clear');
							$('#outputWindow *').trigger('blur');// 离焦
							
							// 刷新表格数据 
							$('#grid').datagrid('reload');
						}else{
							if(data.charAt(0)=='<'){
								$.messager.alert('消息','没有权限 ','info');
							}else{
								$.messager.alert('消息','操作失败 ','info');
							}
						}
							
					});
				}
				
			});	
		
		
	});
	
	
	
	var myview = $.extend({},$.fn.datagrid.defaults.view,{
		onAfterRender:function(target){
			$.fn.datagrid.defaults.view.onAfterRender.call(this,target);
			var opts = $(target).datagrid('options');
			var vc = $(target).datagrid('getPanel').children('div.datagrid-view');
			vc.children('div.datagrid-empty').remove();
			if (!$(target).datagrid('getRows').length){
				var d = $('<div class="datagrid-empty"></div>').html(opts.emptyMsg || 'no records').appendTo(vc);
				d.css({
					position:'absolute',
					left:0,
					top:50,
					width:'100%',
					textAlign:'center'
				});
			}
		}
	});


	$(function () {
       $('#shenqingren').combobox({
        	url:'${pageContext.request.contextPath}/page.kuguanListAll.ashx?refreshcache=new Date().getTime()',
             valueField: 'sId',
            textField: 'sName',
            panelHeight:'300',
            filter:filter,
            onSelect: function (record) {
            	oOBuyer.combobox({
                     url:'${pageContext.request.contextPath}/page.buyerListAll.ashx?creatorId='+record.sId,
                    valueField: 'bId',
                    textField: 'bName',
                    panelHeight:'200'
                }).combobox('clear');
            }
        });
        var oOBuyer = $('#oOBuyer').combobox({
             valueField: 'bId',
            textField: 'bName',
            filter:filter
        });
    });
	
		
</script>	
</head>
<body class="easyui-layout" >
	<div region="center">
	
		<table id="grid">
		
		</table>
	</div>
	
	<div class="easyui-window"  id="outputWindow" collapsible="false" minimizable="false" maximizable="false" style="top:50px;left:200px">
		<div region="north" style="height:31px;overflow:hidden;" split="false" border="false" >
			<div class="datagrid-toolbar">
				<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true" >保存</a>
			</div>
		</div>
		<div region="center" style="overflow:auto;padding:5px;" border="false">
			<form id="sform">
				<table class="table-edit" width="80%" align="center">
					<tr class="title">
						<td colspan="2">入库</td>
					</tr>
					
					
					<tr>
						<td>申请人</td>
 <td>
						<input class="easyui-combobox"    
									required="true"
									name="oBProposerId"
									id="shenqingren"
  									data-options="
 											
											method:'get',
											valueField:'sId',
											textField:'sName',
											panelHeight:'300' 
											
											
						"></td>
					
					</tr>
					<tr>
						<td>经手人</td>
						<td>
						<input class="easyui-combobox"    
									required="true"
									name="oBKuGuanId"
									id="kuguan"
									data-options="
											url:'${pageContext.request.contextPath}/page.kuguanListAll.ashx?refreshcache=new Date().getTime()',
											method:'get',
											valueField:'sId',
											textField:'sName',
											panelHeight:'300' ,
											filter:filter
						"></td>
					</tr>
					
					<tr>
						<td>入库数量</td>
						<td><input name="oBPNumber" type="text" class="easyui-validatebox"  required="true"  /></td>
					</tr>
					<tr>
						<td>入库原因</td>
						<td>
							<select name="oBType" id="select_use">
								   <option value ="0">员工自用退回</option>
								  <option value="1">销售提货退回</option>
							</select> 
							
							
						</td>
					</tr>
					
					
					 <tr id="customer_tr" hidden="hidden"> 
					
						<td>客户名称</td>
					<td>
						<input class="easyui-combobox"    
									name="oBBuyerId"
									id="oOBuyer"
									data-options="
											method:'get',
											valueField:'bId',
											textField:'bName',
											panelHeight:'200'
						"></td>
					</tr>
					
					<tr>
						<td>备注</td>
						<td><input name="oBReMark" type="text"   /></td>
					</tr>
					
				
					</table>
			</form>
		</div>
	</div>
	

	
		<div id="tb" style="padding:5px;height:auto">
		<div style="margin-bottom:5px">
			<a href="#" id="add" class="easyui-linkbutton" iconCls="icon-add" plain="true" >增加</a>
<!-- 			<a href="#" id="edit" class="easyui-linkbutton" iconCls="icon-edit" plain="true">编辑</a>
 -->			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;申请入库人: <input id="searchcontent" class="easyui-textbox" style="width:80px">
			<a href="#"  id="search" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
		
		</div>
	</div>
	
	

	
	
	
</body>
</html>